package com.link.play.fit.ai.bean

import android.text.TextUtils
import com.google.gson.Gson
import com.link.play.fit.ai.utils.DateEx
import java.io.Serializable

data class CollectFoodInfoData(
    var name: String = "",
    var calories: Double = 0.0,
    var carbs: Double = 0.0,
    var protein: Double = 0.0,
    var fats: Double = 0.0,
    var servings: Double = 1.0, //保存的食物的分量
    var img: String = "",
    var type: Int = -1,//1 运动 2 扫描到的食物 3自定义食物 4 接口搜索到的食物

    var description: String = "",
    var timeMillisecond: String = "",
    var ingredients: String = "", // 本地使用的的字段
    var healthScore: Double? = null  //扫描食物要用

) : Serializable

//首页的列表要用的bean
fun CollectFoodInfoData.toRecentlyEatenData(): RecentlyEatenData {
    val red = RecentlyEatenData()
    red.name = name
    red.foodName = name
    red.type = type//食物
    if (red.type == 4) {
        // 接口返回的食物
        red.caloriesDaily = (calories * servings).toInt()
        red.proteinDaily = (protein * servings).toInt()
        red.carbsDaily = (carbs * servings).toInt()
        red.fatsDaily = (fats * servings).toInt()
        runCatching {
            val ing = Gson().fromJson(ingredients, InfoIngredientsBean::class.java)
            ing.servings = servings
            ingredients = Gson().toJson(ing, InfoIngredientsBean::class.java)
        }
        red.ingredients = ingredients
    } else {
        red.caloriesDaily = calories.toInt()
        red.proteinDaily = protein.toInt()
        red.carbsDaily = carbs.toInt()
        red.fatsDaily = fats.toInt()
    }
    red.servings = servings
    red.createTime = timeMillisecond
    red.timeDay = DateEx.getDayOfMonth()
    red.img = img
    red.healthScore=healthScore
    return red
}

//转换成CollectFoodInfoData
fun CollectFoodInfoData.build(
    name: String, num: Double, ingredients: InfoIngredientsBean, type: Int, info: ServingTypeBean
): CollectFoodInfoData {
    this.name = name
    if (ingredients.localCalories != 0.0) {
        this.calories = ingredients.localCalories * ingredients.servings
    } else {
        this.calories = info.calories * ingredients.servings
    }
    if (ingredients.localCarbs != 0.0) {
        this.carbs = ingredients.localCarbs * ingredients.servings
    } else {
        this.carbs = info.carbs * ingredients.servings
    }
    if (ingredients.localProtein != 0.0) {
        this.protein = ingredients.localProtein * ingredients.servings
    } else {
        this.protein = info.protein * ingredients.servings
    }
    this.fats = if (ingredients.localFats != 0.0) {
        ingredients.localFats * ingredients.servings
    } else {
        info.fat * ingredients.servings
    }
    this.servings = ingredients.servings
    this.ingredients = Gson().toJson(ingredients)
    this.type = type
    this.timeMillisecond = ingredients.timeMillisencond
    return this
}

//只有 有ingredients 这个字段的能转成FoodInfoBean
fun CollectFoodInfoData.toFoodInfoBean(): FoodInfoBean? {
    if (TextUtils.isEmpty(ingredients)) return null
    val ing = Gson().fromJson(ingredients, InfoIngredientsBean::class.java)
    val result = FoodInfoBean()
    result.ingredients = mutableListOf<InfoIngredientsBean>().apply { add(ing) }
    return result
}

fun CollectFoodInfoData.toInfoIngredients(): InfoIngredientsBean? {
    runCatching {
        if (TextUtils.isEmpty(ingredients)) return null
        val ing = Gson().fromJson(ingredients, InfoIngredientsBean::class.java)
        return ing
    }
    return null
}


//更新foodinfobean
fun CollectFoodInfoData.updateFoodInfoBean() {
    runCatching {
        if (TextUtils.isEmpty(ingredients)) return
        val ing = Gson().fromJson(ingredients, InfoIngredientsBean::class.java)
        val result = FoodInfoBean()
        ing.calories = calories * servings
        ing.localCalories = calories
        ing.carbs = carbs * servings
        ing.localCarbs = carbs
        ing.protein = protein * servings
        ing.localProtein = protein
        ing.fats = fats * servings
        ing.localFats = fats
        ing.servings = servings
        ingredients = Gson().toJson(ing, InfoIngredientsBean::class.java)
    }
}